﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Projekt
{
    public partial class editShop : Form
    {
        private DataTable shopDataList;
        private string nameInputErrorMsg = "Blank input box not accepted";
        private string phoneNumberInputErrorMsg = "";
        private ToolTip tt = new ToolTip();
        public editShop()
        {
            InitializeComponent();
        }

         /*  ---------------------------------------------
          *  |           Input validating:               |   
          *  |shopName:                                  |
          *  |   not exist in database                   |
          *  |   1 or more character                     |
          *  |shopPhoneNumber:                           |
          *  |   10 or more digit                        |
          *  |   only numeric character beetwen 0 and 9  |
          *  ---------------------------------------------
          *  */
        private void shopPhoneNumber_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                phoneNumberInputErrorMsg = "";
                if (phoneNumberEditBox.Text.ToString().Length > 0)
                {
                    Int64 numberEntered = Int64.Parse(phoneNumberEditBox.Text);
                    if (phoneNumberEditBox.Text.ToString().Length < 10)
                    {
                        phoneNumberInputErrorMsg = phoneNumberInputErrorMsg + "Phone number is 10 or more digit\n";
                        Save.Enabled = false;
                        inputWarning.Visible = true;
                    }
                }
            }
            catch (FormatException es)
            {
                Save.Enabled = false;
                inputWarning.Visible = true;
                phoneNumberInputErrorMsg = phoneNumberInputErrorMsg + "Phone number contains only numeric digits(0..9)";
            }
        }
        private void shopPhoneNumber_Validated(object sender, EventArgs e)
        {
            if (phoneNumberInputErrorMsg == "")
            {
                if (phoneNumberInputErrorMsg == "" && nameInputErrorMsg == "")
                {
                    Save.Enabled = true;
                    inputWarning.Visible = false;
                }
            }
        }
        private void shopName_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (nameEditBox.Text == "")
                {
                    nameInputErrorMsg = "Blank input box not accepted";
                    Save.Enabled = false;
                    inputWarning.Visible = true;
                    return;
                }
                else
                {
                    nameInputErrorMsg = "";
                    foreach (DataRow rows in shopDataList.Rows)
                    {
                        if (rows["UNev"].ToString() == nameEditBox.Text.ToString() && rows["UzletID"].ToString()!=shopID.Text)
                        {
                            nameInputErrorMsg = nameInputErrorMsg + "This shop already exists";
                            Save.Enabled = false;
                            inputWarning.Visible = true;
                            break;
                        }
                    }
                }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.ToString());
            }
        }
        private void shopName_Validated(object sender, EventArgs e)
        {
            if (nameInputErrorMsg == "")
            {
                if (phoneNumberInputErrorMsg == "" && nameInputErrorMsg == "")
                {
                    Save.Enabled = true;
                    inputWarning.Visible = false;
                }
            }
        }

        //***************Validation finish****************************
        private void editShop_Load(object sender, EventArgs e)
        {
            try
            {
                Shop uzlet = new Shop();
                shopDataList = uzlet.getShopList();
                foreach (DataRow rows in shopDataList.Rows)
                {
                    ListViewItem lt = new ListViewItem(rows["UNev"].ToString());
                    lt.SubItems.Add(rows["UCim"].ToString());
                    lt.SubItems.Add(rows["UTelSzam"].ToString());
                    lt.SubItems.Add(rows["UzletID"].ToString());
                    shopList.Items.Add(lt);
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void deleteShopMenu_Click(object sender, EventArgs e)
        {
            try
            {
                Shop uzlet = new Shop();
                ListViewItem item = shopList.FocusedItem;
                String Message = "Do you wan to delete " + item.SubItems[1].Text;
                DialogResult result = MessageBox.Show(Message, "Delete", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    if (uzlet.DeleteShop(item.SubItems[0].Text)) shopList.Items.Remove(item);
                    else MessageBox.Show("This firm can't be deleted. Probably this shop is in use by other tables");
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void nameInput_TextChanged(object sender, EventArgs e)
        {
            try
            {
                Shop uzlet = new Shop();
                shopList.Items.Clear();
                DataTable dt;
                if (nameInput.Text == "" && addressInput.Text == "") dt = uzlet.getShopList();
                else dt = uzlet.SearchShop(nameInput.Text, addressInput.Text);
                foreach (DataRow rows in dt.Rows)
                {
                    ListViewItem lt = new ListViewItem(rows["UNev"].ToString());
                    lt.SubItems.Add(rows["UCim"].ToString());
                    lt.SubItems.Add(rows["UTelSzam"].ToString());
                    shopList.Items.Add(lt);
                }
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); };
        }

        private void addressInput_TextChanged(object sender, EventArgs e)
        {
            nameInput_TextChanged(sender, e);
        }

        private void editShopMenu_Click(object sender, EventArgs e)
        {
            nameEditBox.Visible = true;
            addressEditBox.Visible = true;
            phoneNumberEditBox.Visible = true;
            Save.Visible = true;
            Cancel.Visible = true;
            nameInput.Visible = false;
            addressInput.Visible = false;
            nameInputErrorMsg = "";
            ListViewItem it = shopList.FocusedItem;
            nameEditBox.Text = it.SubItems[0].Text;
            nameEditBox.ReadOnly = true;
            addressEditBox.Text = it.SubItems[1].Text;
            phoneNumberEditBox.Text = it.SubItems[2].Text.Trim();
            shopID.Text = it.SubItems[3].Text;
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            nameEditBox.Visible = false;
            addressEditBox.Visible = false;
            phoneNumberEditBox.Visible = false;
            Save.Visible = false;
            Cancel.Visible = false;
            nameInput.Visible = true;
            addressInput.Visible = true;
            inputWarning.Visible = false;   
        }

        private void nameEditBox_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void addressEditBox_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void phoneNumberEditBox_TextChanged(object sender, EventArgs e)
        {
            try { this.Validate(); }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

        private void tooltpit_reset(object sender, EventArgs e)
        {
            tt.RemoveAll();
        }

        private void InputWarning_MouseOver(object sender, EventArgs e)
        {
            tt = new ToolTip();
            String ttString="";
            if (nameInputErrorMsg != "") ttString = ttString + "\nName: " + nameInputErrorMsg;
            if (phoneNumberInputErrorMsg != "") ttString = ttString + "\nPhone number: " + phoneNumberInputErrorMsg;
            tt.Show(ttString, inputWarning);
        }

        private void shopList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Save_Click(object sender, EventArgs e)
        {
            try
            {
                Shop uzlet = new Shop();
                uzlet.editShop(nameEditBox.Text, addressEditBox.Text, phoneNumberEditBox.Text);
                Cancel_Click(sender, e);
                this.shopList.Items.Clear();
                editShop_Load(sender,e);
            }
            catch (Exception es) { MessageBox.Show(es.ToString()); }
        }

    }
}
